home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / contrib / TLI-patch / src-client / wl-tli.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-06  |  7.3 KB  |  222 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         wl-tcpip.c
  5. * RCS:          $Header: /seq5/u/wolfe/Src/winterp-1.11/src-client/RCS/wl-tli.c,v 1.1 91/04/06 16:15:02 wolfe Exp Locker: wolfe $
  6. * Description:  TCP INET CLIENT to send lisp expressions to WINTERP lisp-server.
  7. *               (this code derived from examples in Sequent DYNIX/ptx ???)
  8. * Author:       David Wolfe, Sequent Computers (after Niels Mayer, HPLabs)
  9. * Created:      Sat Jun 10 02:15:35 1989
  10. * Modified:     Sat Oct  5 23:11:15 1991 (Niels Mayer) mayer@hplnpm
  11. * Language:     C
  12. * Package:      N/A
  13. * Status:       X11r5 contrib tape release
  14. *
  15. * WINTERP Copyright 1989-1991 Hewlett-Packard Company (by Niels Mayer).
  16. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  17. *
  18. * Permission to use, copy, modify, distribute, and sell this software and its
  19. * documentation for any purpose is hereby granted without fee, provided that
  20. * the above copyright notice appear in all copies and that both that
  21. * copyright notice and this permission notice appear in supporting
  22. * documentation, and that the name of Hewlett-Packard and David Betz not be
  23. * used in advertising or publicity pertaining to distribution of the software
  24. * without specific, written prior permission.  Hewlett-Packard and David Betz
  25. * make no representations about the suitability of this software for any
  26. * purpose. It is provided "as is" without express or implied warranty.
  27. *
  28. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  29. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  30. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  31. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  32. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  33. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  34. * PERFORMANCE OF THIS SOFTWARE.
  35. *
  36. * See ./winterp/COPYRIGHT for information on contacting the authors.
  37. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  38. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  39. *
  40. ********************************************************************************
  41. */
  42. static char rcs_identity[] = "@(#)$Header: /seq5/u/wolfe/Src/winterp-1.11/src-client/RCS/wl-tli.c,v 1.1 91/04/06 16:15:02 wolfe Exp Locker: wolfe $";
  43.  
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include <fcntl.h>     
  47. #include <tiuser.h>
  48. #include <netinet/in.h>
  49. #include <netinet/netinet.h>
  50. #include <netdb.h>
  51.  
  52. #include "../src-server/config.h" /* defines DEFAULT_INET_SERVICE_PORT, etc */
  53.  
  54. char *progname = NULL;
  55.  
  56.  
  57. /*******************************************************************************
  58.  * Send <message> to the server via socket <s>
  59.  *******************************************************************************/
  60. void Server_Send(s, message)
  61.      int s;
  62.      char *message;
  63. {
  64.   if (t_snd(s, message, strlen(message), 0) < 0) {
  65.     perror(progname);
  66.     fprintf(stderr, "%s: unable to send() on INET Domain Socket.\n", progname);
  67.     exit(1);
  68.   }
  69. }
  70.  
  71.  
  72. /*******************************************************************************
  73.  * Establish a connection with server process, returning socket descriptor
  74.  * if successful.
  75. *******************************************************************************/
  76. int Server_Connect(serverhost, port)
  77.      char *serverhost;
  78.      ushort port;
  79. {
  80.  
  81.   int fd;            /* connected socket descriptor */
  82.   struct t_call *sndcall;
  83.   struct sockaddr_in *server;
  84.   struct servent *sp;
  85.   struct hostent *hp, *gethostbyname();
  86.     
  87.   /* establish connection */
  88.   if ((fd = t_open(TLI_TCP, O_RDWR, NULL)) < 0) {
  89.     t_error("t_open failed");
  90.     exit(1);
  91.   }
  92.  
  93.   if (t_bind(fd, NULL, NULL) < 0) {
  94.     t_error("t_bind failed");
  95.     exit(2);
  96.   }
  97.  
  98.   if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
  99.     t_error("t_alloc failed");
  100.     exit(3);
  101.   }
  102.  
  103.   hp = gethostbyname(serverhost);
  104.   if (hp == NULL) {
  105.     hp = gethostbyname("localhost");
  106.     if ( hp == NULL ) {
  107.       fprintf(stderr, "gethostbyname failed for %s\n", serverhost);
  108.       exit(3);
  109.     }
  110.   }
  111.  
  112.   server = (struct sockaddr_in *)sndcall->addr.buf;
  113.   server->sin_family = AF_INET;
  114.   server->sin_addr.s_addr = *(unsigned long *)hp->h_addr;
  115.  
  116.   /* select port */
  117.   if (port != 0)
  118.     server->sin_port = htons(port);
  119.   else {
  120.     if ((sp = getservbyname(DEFAULT_INET_SERVICE_NAME, "tcp")) == NULL) {    
  121.       fprintf(stderr, "%s: unable to find \"%s\" in /etc/services.\n",
  122.           progname,
  123.           DEFAULT_INET_SERVICE_NAME);
  124.       exit(1);
  125.     }
  126.     server->sin_port = sp->s_port;
  127.   }
  128.  
  129.   sndcall->addr.len = sizeof(struct sockaddr_in);
  130.     
  131.   if (t_connect(fd, sndcall, NULL) < 0) {
  132.     t_error("t_connect");
  133.     exit(-1);
  134.   }
  135.  
  136.   return(fd);
  137.  
  138. }
  139.  
  140.  
  141. /******************************************************************************
  142.  * Server_Disconnect -- shutdown the connection for further sends. This causes
  143.  * the server to receive an EOF condition after it has received all data from
  144.  * Server_Send().
  145. ******************************************************************************/
  146. void Server_Disconnect(s)
  147.      int s;
  148. {
  149.   /* send disconnect */
  150.   if (t_sndrel(s) < 0) {
  151.     t_error("t_sndrel failed");
  152.     exit(1);
  153.   }
  154. }
  155.  
  156.  
  157. /******************************************************************************
  158.  *
  159.  ******************************************************************************/
  160. main(argc, argv)
  161.      int argc;
  162.      char *argv[];
  163. {
  164.   extern int getopt();        /* proc to get option letter from argv */
  165.   extern char* optarg;        /* arg string being pointed to by getopt() */
  166.   extern int optind;        /* index into argv set by getopt() */
  167.   int opt;            /* option argument */
  168.   char hostname[200];        /* for holding server host name */
  169.   int hostname_given = 0;    /* true if hostname was spec'd by user */
  170.   int s;            /* connected socket desriptor */
  171.   ushort port = 0;        /* port number */
  172.   char* portstr;
  173.   char* hoststr;
  174.  
  175.   progname = argv[0];
  176.  
  177.   while ((opt = getopt(argc, argv, "h:p:")) != EOF)
  178.     switch (opt) {
  179.     case 'h':
  180.       strcpy(hostname, optarg);
  181.       hostname_given = 1;
  182.       break;
  183.     case 'p':
  184.       port = atoi(optarg);
  185.       break;
  186.     case '?':
  187.       fprintf(stderr, "usage: %s [-h hostname] [-p port] [sexpr]\n", progname);
  188.       exit (1);
  189.       break;
  190.     }
  191.  
  192.   if (port == 0) {
  193.     if ((portstr = (char *) getenv(DEFAULT_INET_PORT_ENVVAR)) != NULL) /* DEFAULT_INET_PORT_ENVVAR in "../src-server/config.h" */
  194.       port = (ushort) atoi(portstr);
  195.     else
  196.       port = DEFAULT_INET_SERVICE_PORT; /* default port # for winterp server */
  197.   }
  198.  
  199.   if (hostname_given)
  200.     s = Server_Connect(hostname, port); /* a remote connection w/ host spec'd on command line */
  201.   else {
  202.     if ((hoststr = (char *) getenv(DEFAULT_INET_HOSTADDR_ENVVAR)) != NULL) /* DEFAULT_INET_HOSTADDR_ENVVAR in "../src-server/config.h" */
  203.       s = Server_Connect(hoststr, port); /* use hostname given in environment var */
  204.     else
  205.       s = Server_Connect(NULL, port); /* a local connection -- use loopback */
  206.   }
  207.  
  208.   /*
  209.    * note that due to a bug/feature of winterp, we must send it only
  210.    * one sexp. Here, in the case that multiple args get passed, we also
  211.    * want to ensure that they're whitespace separated for tokenization
  212.    */
  213.   for (; optind < argc; optind++) {
  214.     Server_Send(s, argv[optind]);
  215.     Server_Send(s, " ");
  216.   }
  217.  
  218.   Server_Disconnect(s);
  219.   exit(0);
  220. }
  221.